package cn.nb12.okhttp3.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.nb12.okhttp3.exception.ProjectException;
import cn.nb12.okhttp3.po.Root;
import cn.nb12.okhttp3.service.RetrofitWeatherService;
import cn.nb12.okhttp3.service.WeatherService;

@RestController
@RequestMapping("/weather")
public class WeatherController {

	@Autowired
	private WeatherService weatherService;
	
	@Autowired
	private RetrofitWeatherService retrofitWeatherService;

	/**
	 * 通过okhttp的方式查询
	 * @Description (TODO这里用一句话描述这个方法的作用)
	 * @author:     yhld456 yhld456@hotmail.com
	 * @Date 2017年6月4日 下午3:14:32
	 * @version 1.0.0
	 * @param code
	 * @return
	 */
	@RequestMapping("/okhttp/{code}")
	public ResponseEntity<Map<String, Object>> findByCodeOkhttp3(@PathVariable("code") String code) {

		Map<String, Object> result = new HashMap<>();

		try {
			Root root = weatherService.getFindCodeOkHttp3(code);
			if (root == null) {
				throw new ProjectException("查询失败");
			}

			result.put("status", "1");
			result.put("data", root);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "0");
			result.put("info", "查询失败");
		}
		return new ResponseEntity<>(result, HttpStatus.OK);
	}
	
	/**
	 * 通过Retrofit的方式查询
	 * @Description (TODO这里用一句话描述这个方法的作用)
	 * @author:     yhld456 yhld456@hotmail.com
	 * @Date 2017年6月4日 下午3:14:32
	 * @version 1.0.0
	 * @param code
	 * @return
	 */
	@RequestMapping("/retrofit/{code}")
	public ResponseEntity<Map<String, Object>> findByCodeRetrofit(@PathVariable("code") String code) {

		Map<String, Object> result = new HashMap<>();

		try {
			Root root = retrofitWeatherService.findWeatherByCode(code);
			if (root == null) {
				throw new ProjectException("查询失败");
			}

			result.put("status", "1");
			result.put("data", root);

		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "0");
			result.put("info", "查询失败");
		}
		return new ResponseEntity<>(result, HttpStatus.OK);
	}
}
